package io.renren.roomManager.service.impl;

import cn.lingyueblog.common.utils.PageUtils;
import cn.lingyueblog.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.roomManager.dao.UsersDao;
import io.renren.roomManager.entity.UsersEntity;
import io.renren.roomManager.service.UsersService;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<UsersEntity> wrapper = new QueryWrapper<UsersEntity>();

        if(isNumber(key)){
            wrapper.like("account",key);
        }else if(!isNumber(key) && !key.isEmpty()){
            wrapper.like("name",key);
        }

        IPage<UsersEntity> page = this.page(
                new Query<UsersEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


    @Override
    public void userUpdate(UsersEntity users) {
        users.setUpdatedat(new Date());
        this.updateById(users);
    }

    @Override
    public void userSave(UsersEntity users) {
        users.setCreatedat(new Date());
        users.setUpdatedat(new Date());
        users.setCreatedat(new Date());
        users.setDeletedat(null);
        save(users);
    }

    // 判断字符串是否为数字
    private boolean isNumber(String str) {
        return str.matches("\\d+");
    }

    // 判断字符串是否包含汉字
//    private boolean containsChinese(String str) {
//        return str.matches("[\u4e00-\u9fa5]+");
//    }
}